// Device/pages/History/components/sleep-component/index.js
import dayjs from 'dayjs'
import {
    fetchSleepHistory
} from '@/services/device';
import {
    logger, showToast
} from '@/utils/util';
import {
    customNavigate
} from '@/utils/route'
Component({
    properties: {
        data: {
            type: Object,
            default: null,
            observer(value) {
                this.setData({
                    ...value,
                    start_time: dayjs(new Date()).format('YYYY-MM-DD 00:00:00'),
                    end_time: dayjs(new Date()).format('YYYY-MM-DD 23:59:59')
                }, this.queryList)
            }
        }
    },
    data: {
        minDate: dayjs().subtract(2, 'month').valueOf(),
        maxDate: new Date().getTime(),
        defaultDate: new Date().getTime(),
        start_time: null,
        end_time: null,
        itemTitle: '筛选',
        sleep: [],
        takanap: []
    },
    methods: {
        async queryList() {
            const {
                start_time,
                end_time,
                device_imei
            } = this.data;
            const res = await fetchSleepHistory({
                start_time,
                end_time,
                device_imei
            });
            if (res.return_code == 'SUCCESS') {
                this.setData({
                    sleep: res.data.sleep,
                    takanap: res.data.takanap
                })
            }
        },
        onConfirm() {
            this.setData({
                start_time: dayjs(this.selectComponent('.calendar').data.currentDate).format('YYYY-MM-DD 00:00:00'),
                end_time: dayjs(this.selectComponent('.calendar').data.currentDate).format('YYYY-MM-DD 23:59:59'),
                list: []
            }, () => {
                this.selectComponent('#item').toggle();
                this.queryList()
            })
        },
        onClose() {
            this.setData({
                start_time: undefined,
                end_time: undefined,
                list: []
            }, () => {
                this.selectComponent('#item').toggle();
                this.queryList()
            })
        },
        onNavigate(e) {
            const {
                path
            } = e.currentTarget.dataset;
            customNavigate({
                url: '/Extra/pages/WebView/webview',
                params: {
                    url: path
                }
            })
        }
    }
})